﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VkClientIM.Logic;

namespace VkClientIM.UI
{
	/// <summary>
	/// Interaction logic for ConversationPresenter.xaml
	/// </summary>
	public partial class ConversationPresenter : TabItem
	{
		private Conversation _conversation;

		public ConversationPresenter()
		{
			InitializeComponent();
		}

		public void AttachConversation(Conversation conversation)
		{
			DataContext = conversation;
			_conversation = conversation;
		}


		private void SendClick(object sender, RoutedEventArgs e)
		{
			SendMessage();
		}

		private void SendMessage()
		{
			if (messageTextBox.Text != "")
			{
				_conversation.SendMessage(messageTextBox.Text);
				messageTextBox.Text = "";
			}
		}

		private void messageTextBox_KeyDown(object sender, KeyEventArgs e)
		{
			if ((e.Key==Key.Return)&&(Keyboard.Modifiers  ==  ModifierKeys.Control))
			{
				SendMessage();
			}
			Console.WriteLine("{0}", e.Key);
		}

		private void messagesPresenter_ScrollChanged(object sender, ScrollChangedEventArgs e)
		{
			if (e.ExtentHeightChange > 0.0)
				((ScrollViewer)e.OriginalSource).ScrollToEnd();
		}
	}
}
